<?php
// +----------------------------------------------------------------------
// | ThinkBLOG (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author nicholas <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: AdminBaseAction.class.php 147 2012-06-21 01:24:25Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class AdminBaseAction extends BaseAction {
	
	private $_roles = array ('administrator' => 1, 'editor' => 2, 'author' => 3, 'contributor' => 4, 'subscriber' => 5, 'noaccess' => 6 );
	private $meta;
	
	/**
	 * 初始化
	 *
	 * @access public
	 * @return void
	 */
	public function _initialize() {
		
		parent::_initialize ();
		
		B ( 'AuthCheck' );
		
		/** 个人菜单 */
		$this->menus ();
		
		/** 帮助 */
		$help = '';
		include CONF_PATH . 'help.php';
		
		$this->assign ( 'users', $this->_users );
		$this->assign ( 'meta', $this->_getUserMeta () );
		$this->assign ( 'help', $help [MODULE_NAME] [ACTION_NAME] );
	
	}
	
	public function pass($roleid, $return = false) {
		if ($this->hasLogin ()) {
			if (array_key_exists ( $roleid, $this->_roles ) && $this->_roles [$this->_users ['roleid']] <= $this->_roles [$roleid]) {
				return true;
			}
		} else {
			if ($return) {
				return false;
			} else {
				redirect ( U ( 'Admin/Index/index' ) );
			}
		}
		
		if ($return) {
			return false;
		} else {
			tmessage ( L ( '_ACCESS_DENIED_' ) );
		}
	}
	
	/**
	 * 获取登录ID
	 *
	 * @access protected
	 * @return int
	 */
	protected function _getUserId() {
		return $this->_users ['userid'];
	}
	
	/**
	 * 获取角色ID
	 *
	 * @access protected
	 * @return int
	 */
	protected function _getRoleId() {
		return $this->_users ['roleid'];
	}
	
	/**
	 * 获取登录用户名
	 *
	 * @access protected
	 * @return string
	 */
	protected function _getUserName() {
		return $this->_users ['username'];
	}
	
	/**
	 * 获取用户邮箱
	 *
	 * @access protected
	 * @return string
	 */
	protected function _getEmail() {
		return $this->_users ['email'];
	}
	
	/**
	 * 获取登录用户配置
	 *
	 * @access protected
	 * @return array
	 */
	protected function _getUserMeta() {
		return unserialize ( $this->_users ['meta'] );
	}
	
	/**
	 * 动态菜单
	 *
	 * @access public
	 * @return void
	 */
	public function menus() {
		
		//评论数 系统更新数
		//TODO
		$newcomments = 1;
		$newupdate = 0;
		$modules = $lang = $separator = $purview = $id = '';
		include CONF_PATH . 'menu.php';
		
		foreach ( $modules as $key => $value ) {
			ksort ( $modules [$key] );
		}
		ksort ( $modules );
		
		foreach ( $modules as $key => $val ) {
			$menus [$key] ['label'] = $lang [$key];
			$menus [$key] ['separator'] = isset ( $separator [$key] ) ? $separator [$key] : '';
			$menus [$key] ['css'] = ((strpos ( $key, strtolower ( MODULE_NAME ) ) !== false) || ($key == '02_posts' && strpos ( 'metas', strtolower ( MODULE_NAME ) ) !== false)) ? $css [$key] . 'wp-has-current-submenu wp-menu-open' : $css [$key];
			$menus [$key] ['id'] = $id [$key];
			if (is_array ( $val )) {
				foreach ( $val as $k => $v ) {
					$menus [$key] ['action'] = reset ( $val );
					if (isset ( $purview [$k] )) {
						if (is_array ( $purview [$k] )) {
							$boole = false;
							foreach ( $purview [$k] as $action ) {
								$boole = $boole || Rbac::checkPriv ( $action );
							}
							if (! $boole) {
								continue;
							}
						} else {
							if (! Rbac::checkPriv ( $purview [$k] )) {
								continue;
							}
						}
					}
					$menus [$key] ['children'] [$k] ['label'] = $lang [$k];
					$menus [$key] ['children'] [$k] ['action'] = $v;
					$menus [$key] ['children'] [$k] ['css'] = (strpos ( $k, MODULE_NAME . '_' . ACTION_NAME ) !== false) ? 'class="wp-first-item current"' : '';
				}
			} else {
				$menus [$key] ['action'] = $val;
				$menus [$key] ['children'] = '';
			}
			
			// 如果children的子元素长度为0则删除该组
			if (empty ( $menus [$key] ['children'] )) {
				unset ( $menus [$key] );
			}
		}
		
		$this->assign ( 'menus', $menus );
	}
	
	//public function index() {
	//		$map = $this->_search ();
	//		if (method_exists ( $this, '_filter' )) {
	//			$this->_filter ( $map );
	//		}
	//		$model = M ( $this->getActionName () );
	//		if (! empty ( $model )) {
	//			$this->_list ( $model, $map );
	//		}
	//		$this->display ();
	//		return;
	//	}
	

	protected function _search($name = '') {
		if (empty ( $name )) {
			$name = $this->getActionName ();
		}
		$model = M ( $name );
		$map = array ();
		foreach ( $model->getDbFields () as $key => $val ) {
			if (substr ( $key, 0, 1 ) == '_')
				continue;
			if (isset ( $_REQUEST [$val] ) && $_REQUEST [$val] != '') {
				$map [$val] = $_REQUEST [$val];
			}
		}
		return $map;
	}
	
	/**
	 * 根据表单生成查询条件
	 * 进行列表过滤
	 *
	 * @access protected
	 * @param Model $model 数据对象
	 * @param HashMap $map 过滤条件
	 * @param string $sortBy 排序
	 * @param boolean $asc 是否正序
	 *
	 * @return void
	 */
	protected function _list($model, $map = array(), $sortBy = '', $asc = false) {
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['order'] )) {
			$order = $_REQUEST ['order'];
		} else {
			$order = ! empty ( $sortBy ) ? $sortBy : $model->getPk ();
		}
		//排序方式 默认按照倒序排列
		if (isset ( $_REQUEST ['sort'] )) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}
		if (! empty ( $_REQUEST ['map'] )) {
			$map = json_decode ( base64_url_decode ( $_REQUEST ['map'] ), true );
		}
		//取得满足条件的记录数
		$count = $model->where ( $map )->count ();
		
		$p = new Page ( $count, C ( 'ADMIN_LIST_NUM' ), array ('map' => base64_url_encode ( json_encode ( $map ) ), 'order' => $order, 'sort' => $sort ) );
		
		//分页查询数据
		$list = $model->getListByMap ( $map, '*', "`" . $order . "` " . $sort, $p->firstRow . ',' . $p->listRows );
		
		$p->setConfig ( 'prev', "‹‹" );
		$p->setConfig ( 'next', '››' );
		$p->setConfig ( 'first', '1 ...' );
		$p->setConfig ( 'last', '... ' );
		$p->setConfig ( 'theme', C ( 'ADMIN_PAGE_THEME' ) );
		
		//分页显示
		$page = $p->show ();
		
		$sort = $sort . '_' . $order;
		
		$this->assign ( 'page', $page );
		$this->assign ( 'list', $list );
		$this->assign ( 'sort', $sort );
		$this->assign ( 'order', ($_REQUEST ['sort']) ? 'sorted ' . $_REQUEST ['sort'] : NULL );
		//Cookie::set ( '_currentUrl_', __SELF__ );
		return;
	}
	
	public function add() {
		$this->display ();
	}
	
	public function batch() {
		
		$data = $this->r->from ( 'action' );
		if (! empty ( $data ['action'] )) {
			switch ($data ['action']) {
				case 'delete' :
					$this->delete ();
					break;
				case 'refresh' :
					$this->refresh ();
					break;
			}
		}
		redirect ( cookie ( 'currentUrl_' ) );
	}
	
	//彻底删除记录
	public function delete() {
		$model = M ( $this->getActionName () );
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			if (isset ( $id )) {
				$condition = array ($pk => array ('in', $id ) );
				if (false !== $model->where ( $condition )->delete ()) {
					$this->assign ( 'success', L ( '设置已保存。' ) );
				} else {
					$this->error = '<strong>错误</strong>：未知错误。';
				}
			} else {
				$this->error = '<strong>错误</strong>：非法操作。';
			}
		}
	}

}